package com.example.startapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import com.example.startapp.R;
import com.example.startapp.adapter.GuideAdapter;

import java.util.ArrayList;
import java.util.List;

public class GuideActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private GuideAdapter guideAdapter;
    private List<View> views;
    private int[] pics= {R.layout.guide_one, R.layout.guide_two, R.layout.guide_three};

    private TextView[] textViews;
    private TextView one,two,three;
    TextView tskip;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_guide);

        init();

    }

    private void init(){
        viewPager = findViewById(R.id.view_pager);
        one = findViewById(R.id.one);
        two = findViewById(R.id.two);
        three = findViewById(R.id.three);
        tskip = findViewById(R.id.start_home);
        tskip.setOnClickListener(cl);

        if (views == null) {
            views = new ArrayList<>();

        }
        if (textViews == null) {
            textViews = new TextView[3];

        }
        textViews[0] = one;
        textViews[1] = two;
        textViews[2] = three;
        //设置第一个点orange
        setPointSelect(true, 0);
        for (int i = 0; i < pics.length; i++) {
            View view = LayoutInflater.from(this).inflate(pics[i], null);
            views.add(view);
        }
        guideAdapter = new GuideAdapter(views);
        viewPager.addOnPageChangeListener(vp);
        viewPager.setAdapter(guideAdapter);
        Button button=views.get(2).findViewById(R.id.btn);
        button.setOnClickListener(cl);



    }
    ViewPager.OnPageChangeListener vp=new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            //引导点变化
            setPointSelect(false,position);
            if (position==2){
                tskip.setVisibility(View.GONE);
            }else {
                tskip.setVisibility(View.VISIBLE);
            }

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };




    //跳转到主页界面
    View.OnClickListener cl=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent=new Intent(GuideActivity.this,MainActivity.class);
            startActivity(intent);

        }
    };

    //设置引导点未选中
    private void setPointSelect(boolean state, int position) {
        for (int i = 0; i < textViews.length; i++) {
            textViews[i].setBackground(getDrawable(R.color.gray));
        }
        if (state) {
            //第一张为选中状态
            textViews[0].setBackground(getDrawable(R.color.orange));

        } else {
            textViews[position].setBackground(getDrawable(R.color.orange));

        }

    }
}
